package jcu.ict.profaid;


import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SuppressLint("NewApi")
public class FormEditCourse extends Activity {
	private static CourseDbAdapter cdb;
	private String University;
	private String Program;
	private String Ccode;
	private String Cname;
	private String Notes;
	private String course_id;
	private String i;

	@SuppressLint("NewApi")
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.form_add_subject);
			
		Bundle bundle = this.getIntent().getExtras();
		course_id = bundle.getString("setInfoCourse");
		i = (String) bundle.getCharSequence("setUserId");
		 
		ActionBar actionBar = getActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);
		ColorDrawable colorDrawable = new ColorDrawable(
				Color.parseColor("#0099CC"));
		actionBar.setBackgroundDrawable(colorDrawable);

		final EditText edt_university = (EditText) findViewById(R.id.edtUniversity);
		final EditText edt_program = (EditText) findViewById(R.id.edtProgram);
		final EditText edt_Ccode = (EditText) findViewById(R.id.edtCourseCode);
		final EditText edt_Cname = (EditText) findViewById(R.id.edtCourseName);
		final EditText edt_notes = (EditText) findViewById(R.id.edtNotes);

		cdb = new CourseDbAdapter(getBaseContext());
		Button btnSave = (Button) findViewById(R.id.btnYes);
		Button btnClose = (Button) findViewById(R.id.btnNo);
		
		btnSave.setText("Edit");
		
		cdb.open();
		Cursor xCursor = cdb.getEditCourse(course_id);
		xCursor.moveToFirst();
		
		University = xCursor.getString(0);
		edt_university.setText(University);
		
		Program = xCursor.getString(1);
		edt_program.setText(Program);
		
		Ccode = xCursor.getString(2);
		edt_Ccode.setText(Ccode);
		
		Cname = xCursor.getString(3);
		edt_Cname.setText(Cname);
		
		Notes = xCursor.getString(4);
		edt_notes.setText(Notes);
		
		cdb.close();
		
		btnSave.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				University = edt_university.getText().toString();
				Program = edt_program.getText().toString();
				Ccode = edt_Ccode.getText().toString();
				Cname = edt_Cname.getText().toString();
				Notes = edt_notes.getText().toString();
				
				cdb.open();
				if(cdb.updateAllCourse(course_id, University, Program, Ccode, Cname, Notes)){
					Toast.makeText(getBaseContext(), "Update success!", Toast.LENGTH_LONG).show();
					Intent in = new Intent(FormEditCourse.this, MainCourse.class);
					Bundle bundle = new Bundle();
					bundle.putCharSequence("setUserId", i);
					in.putExtras(bundle);
					startActivity(in);
					finish();
				}
				else{
					Toast.makeText(getBaseContext(), "Update Failed! Please Try Again...", Toast.LENGTH_LONG).show();
					Intent in = new Intent(FormEditCourse.this, MainCourse.class);
					Bundle bundle = new Bundle();
					bundle.putCharSequence("setUserId", i);
					in.putExtras(bundle);
					startActivity(in);
					finish();
				}
				cdb.close();
				
			}
		});
		
		btnClose.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Intent in = new Intent(FormEditCourse.this, MainCourse.class);
				Bundle bundle = new Bundle();
				bundle.putCharSequence("setUserId", i);
				in.putExtras(bundle);
				startActivity(in);
				finish();
			}
		});
	}
	
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			Intent in = new Intent(FormEditCourse.this, MainCourse.class);
			Bundle bundle = new Bundle();
			bundle.putCharSequence("setUserId", i);
			in.putExtras(bundle);
			startActivity(in);
			finish();
		}
		return true;
	}
	
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// app icon in action bar clicked; go home
			Intent in = new Intent(FormEditCourse.this, MainCourse.class);
			in.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			Bundle bundle = new Bundle();
			bundle.putCharSequence("setUserId", i);
			in.putExtras(bundle);
			startActivity(in);
			finish();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
}